package com.maila.calltaxi_driver.ui;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.maila.calltaxi_driver.R;
import com.maila.calltaxi_driver.utils.StartActivityUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/3.
 */

public class RegisterScendActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.title_login)
    TextView titleLogin;
    @BindView(R.id.register_header)
    AutoRelativeLayout registerHeader;
    @BindView(R.id.enter_phone_number)
    EditText enterPhoneNumber;
    @BindView(R.id.delete_number_iv)
    ImageView deleteNumberIv;
    @BindView(R.id.all_zhuce)
    AutoLinearLayout allZhuce;
    @BindView(R.id.bt_getcode)
    Button btGetcode;
    @BindView(R.id.phone_num)
    TextView phone_num;

    private Timer mTimer;
    private int countdown = 60;
    protected boolean isSendSuccess = false;
    private boolean codeOK = false;
    String num="";
    private Handler mHandler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            if (msg.what == 0) {
                isSendSuccess = false;
                enterPhoneNumber.setText("");
                btGetcode.setEnabled(true);
                btGetcode.setClickable(true);
                btGetcode.setBackgroundResource(R.drawable.tv_bg_cz_gree);
                btGetcode.setText("重新发送");
                if (mTimer != null) {
                    mTimer.cancel();
                    mTimer = null;
                }
            } else {
                btGetcode.setEnabled(false);
                btGetcode.setBackgroundResource(R.drawable.buttonfillet);
                btGetcode.setText(msg.what + "秒");
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registerscend_activity);
        ButterKnife.bind(this);
        btGetcode.setEnabled(false);
        btGetcode.setClickable(false);
        num=getIntent().getStringExtra("str");
        phone_num.setText(num);
        countdown = 60;
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (countdown >= 0) {
                    mHandler.sendEmptyMessage(countdown);
                    countdown--;
                }
            }
        }, 0, 1000);
    enterPhoneNumber.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(s.length()==4){
                Toast.makeText(RegisterScendActivity.this,"验证成功",Toast.LENGTH_SHORT).show();
                StartActivityUtils.start(RegisterScendActivity.this,RegisterThreActivity.class);
                finish();
            }
        }
    });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    @OnClick({R.id.white_return_iv, R.id.delete_number_iv, R.id.bt_getcode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.delete_number_iv:
                enterPhoneNumber.setText("");
                break;
            case R.id.bt_getcode:
                btGetcode.setEnabled(false);
                btGetcode.setClickable(false);
                countdown = 60;
                mTimer = new Timer();
                mTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if (countdown >= 0) {
                            mHandler.sendEmptyMessage(countdown);
                            countdown--;
                        }
                    }
                }, 0, 1000);
                break;
        }
    }
}
